package com.woniu.cloud.storeassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TStoreServiceMiddle;
import com.woniu.cloud.form.TStoreServiceUpdateMiddle;
import com.woniu.cloud.storeassembly.mapper.TStoreServiceMiddleMapper;
import com.woniu.cloud.storeassembly.service.ITStoreServiceMiddleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 店铺服务中间表  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-22
 */
@Service
public class TStoreServiceMiddleServiceImpl extends ServiceImpl<TStoreServiceMiddleMapper, TStoreServiceMiddle> implements ITStoreServiceMiddleService {


    @Resource
    private TStoreServiceMiddleMapper storeServiceMiddleMapper;

    @Override
    public Result InsertStoreServiceMiddle(TStoreServiceMiddle serviceMiddle) {

        serviceMiddle.setStoreServiceMiddleTotal(0.0)
               .setStoreServiceMiddleNum(0)
               .setStoreServiceMiddleCreateTime(new Date());
        int insert = storeServiceMiddleMapper.insert(serviceMiddle);
        if (0!=insert) {
            return Result.success("添加成功",null);
        }
        return Result.fail("添加失败",null);
    }

    @Override
    public Result updateStoreServiceMiddleById(TStoreServiceUpdateMiddle serviceUpdateMiddle) {
        UpdateWrapper<TStoreServiceMiddle> wrapper = new UpdateWrapper<>();
        wrapper.eq("store_service_middle_id",serviceUpdateMiddle.getStoreServiceMiddleId())
                .set(null!=serviceUpdateMiddle.getSecondServiceName()&&""!=serviceUpdateMiddle.getSecondServiceName(),"second_service_name",serviceUpdateMiddle.getSecondServiceName())
                .set(null!=serviceUpdateMiddle.getStoreServiceMiddleCommition()&&0.0!=serviceUpdateMiddle.getStoreServiceMiddleCommition(),"store_service_middle_commition",serviceUpdateMiddle.getStoreServiceMiddleCommition())
                .set(null!=serviceUpdateMiddle.getStoreServiceMiddleStatus(),"store_service_middle_status",serviceUpdateMiddle.getStoreServiceMiddleStatus())
                .set(null!=serviceUpdateMiddle.getSecondServicePrice(),"second_service_price",serviceUpdateMiddle.getSecondServicePrice())
                .set(null!=serviceUpdateMiddle.getStoreServiceMiddleNum(),"store_service_middle_num",serviceUpdateMiddle.getStoreServiceMiddleNum())
                .set(null!=serviceUpdateMiddle.getStoreServiceMiddleTotal(),"store_service_middle_total",serviceUpdateMiddle.getStoreServiceMiddleTotal());
        int update = storeServiceMiddleMapper.update(null, wrapper);
        if (0!=update) {
            return Result.success("修改成功",null);
        }
        return Result.fail("修改失败",null);

    }

    @Override
    public TStoreServiceMiddle getStoreServiceMiddleById(Integer id) {
        TStoreServiceMiddle storeServiceMiddle = getBaseMapper().selectById(id);
        return storeServiceMiddle;

    }


    @Override
    public Result getStoreServiceMiddleListByStoreId(Integer storeId) {
        QueryWrapper<TStoreServiceMiddle> wrapper = new QueryWrapper<>();
        wrapper.eq("store_service_middle_store_id",storeId);

        List<TStoreServiceMiddle> list = storeServiceMiddleMapper.selectList(wrapper);
        if (0!=list.size()) {
            return Result.success("数据存在",list);
        }
        return Result.fail("该店铺暂无服务",null);
    }

    @Override
    public Result updateStoreServiceMiddleStatusByStoreServiceMiddleId(Integer storeServiceMiddleId,Integer status) {
        UpdateWrapper<TStoreServiceMiddle> wrapper = new UpdateWrapper<>();
        wrapper.eq("store_service_middle_id",storeServiceMiddleId)
                .set("store_service_middle_status",status);
        int update = storeServiceMiddleMapper.update(null, wrapper);
        if (0!=update) {
            return Result.success("状态修改成功",null);
        }
        return Result.fail("状态修改失败",null);
    }

}
